#ifndef TEXTEDITOR_H
#define TEXTEDITOR_H
#include <QHash>
#include <Qsci/qsciscintilla.h>
#include "Include/IEditor.h"
#include "Include/IEditors.h"

class ITheme;
class StatusBar;
class QAbstractTableModel;

namespace Lexers {
class ILexer;
}

namespace Plugins
{

class Project;

class TextEditor : public QsciScintilla, public Plugins::IEditor
{
    Q_OBJECT
    Q_INTERFACES(Plugins::IEditor)
public:
    explicit TextEditor(QWidget *parent = 0);
    virtual ~TextEditor();
    virtual bool save();
    virtual bool load(const QString & path);
    virtual QWidget * widget();
    virtual void setLexerByName(const QString& lexName);
    virtual void setText(const QString& text);

    void init(const QString & lexName);
    virtual void activate();
    virtual QString selectedText();
    virtual void find(const QString& find, bool forward);
    virtual void replace(const QString& find, const QString& replace, bool findNext, bool forward);
    virtual void replaceAll(const QString& find, const QString& replace);
    virtual QsciLexer * getLexer();
    virtual void setStatusBar(StatusBar * bar);
    virtual void setProject(Project *prj);
    virtual bool isModified();

    void allocateId(int &id, unsigned &allocated, int min, int max);
    void checkIndicatorOld(int &indicatorNumber);
    int indicatorDefineOld(long style, int indicatorNumber = -1);
    bool indicatorDrawUnderOld(int indicatorNumber) const;
    void setIndicatorForegroundColorOld(const QColor &col, int indicatorNumber);
    void setIndicatorDrawUnderOld(bool under, int indicatorNumber);
    void fillIndicatorRange(int lineFrom, int indexFrom, int lineTo, int indexTo, int indicatorNumber);
    virtual void setStatusBarVisible(bool = true){;}
protected:
    virtual void onLoad(){}
    virtual void onSave(){}
    void clearErrors();
    void markErrors(int lineNo, const QString & message, int sev);
    bool event(QEvent *event);
signals:
    void undoAvailable(bool);
    void redoAvailable(bool);
    void pasteAvailable(bool);
    void severityChanged(int sev);
    void errorListChanged(QAbstractTableModel*);
    void fileNavigate(const QString &, int);
public slots:
    virtual void undo();
    virtual void redo();
    virtual void copy();
    virtual void cut();
    virtual void paste();
    virtual void del();
    virtual void selectAll();

    virtual void uppercase();
    virtual void lowercase();
    virtual void capitalize();
    virtual void setTheme(ITheme * theme = NULL, QFont * fnt = NULL);
    virtual bool reset();
    virtual void jumpLine(int);
    virtual void jumpOffset(int);
protected slots:
    void onModificationChanged(bool m);
    void onSelectionChanged();
    void onCursorPositionChanged(int, int);
    virtual void onCodeComplite(){}
private:
    void loadDocument();
protected:
    QString _fileName;
    Lexers::ILexer *_lex;
    StatusBar *_bar;
    Project *_project;
    int _errMarker, _warnMarker, _convMarker;
    QHash<int, QString> _warnings;
    unsigned allocatedIndicatorsOld;
};

}

#endif // TEXTEDITOR_H
